<?php
require_once(DP_BASE_DIR."/modules/projects/items/projectitems.class.php");
$pitem_id = intval( dPgetParam( $_GET, 'pitem_id', 0 ) );
$pitem_project = intval( dPgetParam( $_GET, 'pitem_project', 0) );
// check permissions for this record
$perms =& $AppUI->acl();
$canEdit = $perms->checkModuleItem( $m, 'edit', $AppUI->user_id);
// pull the related project
$project = new CProject();
$project->load( $pitem_project);

$canEdit = $canEdit && $project->canEdit();

if (!$canEdit) {
    $AppUI->setMsg('Access denied', UI_MSG_ERROR);
    $AppUI->redirect();
}

// load the record data
$msg = '';
$row = new CMdProject_items();

$canDelete = $row->canDelete( $msg, $pitem_id );
if($msg == $AppUI->_('Project_itemsDeleteUserError', UI_OUTPUT_JS)) {
    $userDeleteProtect=true;
}

if ($view) {
    $view = ' readonly="true"';
}
else {
    unset($view);
}

if (!$row->load( $pitem_id ) && $pitem_id > 0) {
    $AppUI->setMsg( 'Project_items' );
    $AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
    $AppUI->redirect();
}
require_once($AppUI->getModuleClass("items"));
$item = new CItem();
if($row->pitem_item_id) {
	$item->load($row->pitem_item_id);
}
if (!$view) {

require_once($AppUI->getMasterDataClass('mdcurrency'));
$currency_list = arrayMerge(array( '' => '' ), getCurrencyList());

require_once($AppUI->getMasterDataClass('mdtype'));
//$container_type_list = getTypeList('Container');
$container_type_list =  getTypeList('Container');

$q = new DBQuery();
$q->addTable('project_items');
$q->addWhere('pitem_project = ' . $pitem_project);
$q->addQuery('distinct pitem_item_id, pitem_BL_AW, pitem_invoice_no, pitem_PO_no');
$prevList = $q->loadList('pitem_item_id');

if(!isset($row->pitem_item_id) || $row->pitem_item_id <= 1){
	$row->pitem_BL_AW = @$prevList[0]['pitem_BL_AW'];
	$row->pitem_invoice_no = @$prevList[0]['pitem_BL_AW'];
	$row->pitem_PO_no = @$prevList[0]['pitem_PO_no'];
}

// Load all item list
require_once($AppUI->getModuleClass('items'));
$iO = new CItem();
$itemList = $iO->getItemsList4AutoCompleter('ReferenceLine');

require_once($AppUI->getMasterDataClass('mdunit'));
$qunit_list = getUnitList('quantity');
$wunit_list = getUnitList('weight');
$vunit_list = getUnitList('volume');

?>
<form name="MdProject_itemsForm" action="?m=projects&u=items" method="post">
<input type="hidden" name="dosql" value="projectitems_do_aed" />
<input type="hidden" name="pitem_id" value="<?=$pitem_id?>" />
<input type="hidden" name="pitem_project" value="<?=$pitem_project?>" />

<?}?>
<table border="0" cellpadding="4" cellspacing="0" width="100%">
<tr>
	<td class="tabon"><?=$project->project_name?> :: <?=$pitem_id > 0 ? $AppUI->_('Edit line item') : $AppUI->_('New line item')?></td>
	<td width="100%">&nbsp;</td>
</tr>
</table>
<table border="0" cellpadding="4" cellspacing="0" width="100%" class="infopanel">
<tr>
	<td nowrap="nowrap">
		<input class="button" type="submit" name="save"
				value="<?=$AppUI->_('save')?>" onClick="return doSubmit();"/>
		<input class="button" type="button" name="cancel"
				value="<?=$AppUI->_('cancel')?>" onClick="doCancel()" />
	</td>
	<td align="right" width="100%">
	<?
		if ($canReadProject) {
			showButton( 'view this reference', '?m=projects&a=view&project_id='.$task_project );
		}
		if ($task_id > 0) {
			showButton( 'view this task', '?m=tasks&a=view&task_id='.$obj->task_id );
		}
	?>&nbsp;
	</td>
</tr>
</table>
<table border="0" cellpadding="10" cellspacing="4" width="100%" class="subinfopanel">
<tr>
<td width="580px" valign="top" id="idColumnLeft">
<table border="0" cellpadding="4" cellspacing="0" >
<tr>
    <td valign="top">
        <table border="0" cellpadding="0" cellspacing="4" width="100%">
        <tr>
	        <td align="right" nowrap="nowrap">* <?=$AppUI->_('Item code');?></td>
			<td>
				<!--
				<input type="hidden" id="idProjectItemId" name="pitem_item_id" value="<?=@$row->pitem_item_id?>" />
				<input id="idProjectItemCode" tabindex="1" type="text" class="text" name="pitem_item_code"
					value="<?=@$item->item_code?>" style="width: 280px" maxlength="255" readonly="true"/>
				<img class="clickable" src="images/treetable.gif" onclick="doShowItemsLiveSearchBox()"/>
				-->
				<?= arrayAutoSelectLocalItem($itemList, 'pitem_item_id', ' style="width: 280px" maxlength="255"', @$row->pitem_item_id, false, @$item->item_code, 'doSelectItem');?>
			</td>
		</tr>
        <tr>
	        <td align="right" nowrap="nowrap">* <?=$AppUI->_('Item name');?></td>
			<td>
				<input id="idProjectItemName" title="Item name :: required" type="text" class="text" name="pitem_name"
					value="<?=@$item->item_name?>" style="width: 280px" maxlength="255" readonly="true"/>
			</td>
		</tr>
        <tr>
            <td align="right"><?=$AppUI->_('B/L-A/W No.')?></td>
            <td>
                <input type="text" class="text" id="pitem_BL_AW" name="pitem_BL_AW"
                value="<?=@$row->pitem_BL_AW?>"  style="width: 100%" <?=$view?>/>
            </td>
        </tr>
        <tr>
            <td align="right">* <?=$AppUI->_('Invoice No.')?></td>
            <td>
                <input type="text" class="text" id="idPItemInvoiceNo" title="Item Invoice No :: Please input one of Invoice NO. or PO NO." name="pitem_invoice_no"
                value="<?=@$row->pitem_invoice_no?>"  style="width: 100%" <?=$view?>/>
            </td>
        </tr>
        <tr>
        	<td align="right"><?=$AppUI->_('Invoice amount')?></td>
            <td nowrap>
                <input type="text" class="text" id="idPItemInvoiceAmount" name="pitem_invoice_amount"
                value="<?=@$row->pitem_invoice_amount?>"  style="text-align: right; width: 40%" <?=$view?> title="Invoice amount :: If you input invoice amount then please select currency"/>
            	<?=$AppUI->_('Currency')?>
            	<!-- title="Currency :: Currecy for invoice amount" -->
            	<? echo arraySelect($currency_list,'pitem_currency','id="idPItemCurrency" class="text" ',@$row->pitem_currency)?>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('P/O No.')?></td>
            <td>
                <input type="text" class="text" id="idPItemPONo" name="pitem_PO_no"
                value="<?=@$row->pitem_PO_no?>" title="Item Invoice No :: Please input one of Invoice NO. or PO NO."  style="width: 100%" <?=$view?>/>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('C/O No.')?></td>
            <td>
                <input type="text" class="text" name="pitem_CO_no"
                value="<?=@$row->pitem_CO_no?>"  style="width: 100%" <?=$view?>/>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Contract No.')?></td>
            <td>
                <input type="text" class="text" name="pitem_contract_no"
                value="<?=@$row->pitem_contract_no?>"  style="width: 100%" <?=$view?>/>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Quantity')?></td>
            <td nowrap>
                <input type="text" class="text" name="pitem_quantity" style="text-align: right; width: 80%"
                value="<?=@$row->pitem_quantity?>"  <?=$view?>/>
                <? echo arraySelect($qunit_list,'pitem_quantity_unit','id="pitem_quantity_unit" class="text" style="width:60px"',@$row->pitem_quantity_unit)?>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Weight')?></td>
            <td nowrap>
				<input type="text" class="text" name="pitem_weight" style="text-align: right; width: 80%"
                value="<?=@$row->pitem_weight?>"  <?=$view?>/>
                <? echo arraySelect($wunit_list,'pitem_weight_unit','id="pitem_weight_unit" class="text" style="width:60px"',@$row->pitem_weight_unit)?>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Volume')?></td>
            <td nowrap>
                <input type="text" class="text" name="pitem_volume" style="text-align: right; width: 80%"
                value="<?=@$row->pitem_volume?>" <?=$view?>/>
                <? echo arraySelect($vunit_list,'pitem_volume_unit','id="pitem_volume_unit" class="text" style="width:60px" ',@$row->pitem_volume_unit)?>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Container')?></td>
            <td nowrap="nowrap">
                <?= arraySelect($container_type_list,'pitem_container','style="width: 60%"',@$row->pitem_container)?>
                <input type="text" class="text" name="pitem_container_total" style="text-align: right; width: 40%"
                value="<?=@$row->pitem_container_total?>" <?=$view?>/>
            </td>
        </tr>
    </table>
    </td>
    <td valign="top">
    <table border="0" cellpadding="0" cellspacing="4" width="100%">

		<!-- new td-->

        <tr>
            <td align="right"><?=$AppUI->_('Package')?></td>
            <td>
                <input type="text" class="text" name="pitem_package"
                value="<?=@$row->pitem_package?>"  style="width: 100%" <?=$view?>/>
            </td>
        </tr>
        <tr>
            <td align="right">* <?=$AppUI->_('Stamp date')?></td>
            <td>
            	<input id="idDateStamp" type="hidden" name="pitem_stamp_date"
				value="<?=@$row->pitem_stamp_date?>" />
				<input id="idDateStampD" title="Stamp date :: required" type="text" style="width: 60px"
						value="<?=@$row->pitem_stamp_date?>"
						class="text" disabled="disabled" />
				<img id="idDateStampB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>" border="0" style="vertical-align: text-bottom"/>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Stamp time')?></td>
            <td>
                <input type="text" class="text" name="pitem_stamp_time"
                value="<?=@$row->pitem_stamp_time?>"  style="width: 100%" <?=$view?>/>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Customs Dec No.')?></td>
            <td>
                <input type="text" class="text" name="pitem_customs_dec_no"
                value="<?=@$row->pitem_customs_dec_no?>"  style="width: 100%" <?=$view?>/>
            </td>
        </tr>
        <tr>
            <td align="right">* <?=$AppUI->_('Custom date')?></td>
            <td>
            	<input id="idDateCustom" title="Custome date :: required" type="hidden" name="pitem_custom_date"
				value="<?=@$row->pitem_custom_date?>" />
				<input id="idDateCustomD" type="text" style="width: 60px"
						value="<?=@$row->pitem_custom_date ?>"
						class="text" disabled="disabled" />
				<img id="idDateCustomB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>" border="0" style="vertical-align: text-bottom"/>
            </td>
        </tr>
        <tr>
            <td align="right">* <?=$AppUI->_('ETA date')?></td>
            <td>
            	<input id="idDateETA" title="ETA date :: required" type="hidden" name="pitem_ETA_date"
				value="<?=@$row->pitem_ETA_date?>" />
				<input id="idDateETAD" type="text" style="width: 60px"
						value="<?=@$row->pitem_ETA_date?>"
						class="text" disabled="disabled" />
				<img id="idDateETAB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>" border="0" style="vertical-align: text-bottom"/>
            </td>
        </tr>
        <tr>
            <td align="right">* <?=$AppUI->_('Delivery date')?></td>
            <td>
            	<input id="idDateDelivery" title="Delivery date :: required" type="hidden" name="pitem_delivery_date"
				value="<?=@$row->pitem_delivery_date?>" />
				<input id="idDateDeliveryD" type="text" style="width: 60px"
						value="<?=@$row->pitem_delivery_date?>"
						class="text" disabled="disabled" />
				<img id="idDateDeliveryB" src="images/calendar.gif" title="<?=$AppUI->_('Calendar')?>" border="0" style="vertical-align: text-bottom"/>
            </td>
        </tr>
        <tr>
            <td align="right">* <?=$AppUI->_('Delivery location')?></td>
            <td>
                <input type="text" class="text" name="pitem_delivery_location" id="pitem_delivery_location" title="Delivery location :: required"
                value="<?=@$row->pitem_delivery_location?>"  style="width: 100%" <?=$view?>/>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Inspection')?></td>
            <td>
                <input type="checkbox" class="text" name="pitem_inspection" id="pitem_inspection"
                value="1" <?=$view?> <?= (@$row->pitem_inspection == 1) ? "checked='true'":""?>/>
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Unloading')?><?=@$row->pitem_unloading?></td>
            <td>
                <input type="checkbox" class="text" name="pitem_unloading"
                value="1"  <?=$view?> <?= (@$row->pitem_unloading == 1) ? "checked='true'":""?> />
            </td>
        </tr>
        <tr>
            <td align="right"><?=$AppUI->_('Remarks')?></td>
            <td>
				<textarea name="pitem_remarks" class="text" rows="6" cols="40" wrap="on"><?=@$row->pitem_remarks?></textarea>
            </td>
        </tr>
        </table>
    </td>
</tr>
</table>
</td>
</tr>
</table>
<table border="0" cellpadding="4" cellspacing="0" width="100%" class="infopanel">
<tr>
	<td nowrap="nowrap">
		<input class="button" type="submit" name="save"
				value="<?=$AppUI->_('save')?>" onClick="return doSubmit();"/>
		<input class="button" type="button" name="cancel"
				value="<?=$AppUI->_('cancel')?>" onClick="doCancel()" />
	</td>
	<td align="right" width="100%">
	<?
		if ($canReadProject) {
			showButton( 'view this reference', '?m=projects&a=view&project_id='.$task_project );
		}
		if ($task_id > 0) {
			showButton( 'view this task', '?m=tasks&a=view&task_id='.$obj->task_id );
		}
	?>&nbsp;
	</td>
</tr>
</table>
</form>
<div id="idItemsLiveSearchBox" class="popup" style="visibility: hidden; left: 436px; top: 94.5px; display: none;">
<?php $screen = 'ReferenceLine'; require_once(DP_BASE_DIR."/modules/items/item_livesearch.php");?>
</div>

<script language="javascript">

function doShowItemsLiveSearchBox() {
	new Dialog('idItemsLiveSearchBox',
		function(){
			Dialog('idItemsLiveSearchBox').hide();
		}).show();
}

function doSelectItem(itemObj) {
	itemObj = itemObj || {id:'', code:'', name:'', type:'', quantity:'', weight:'', volume:''}

	$('pitem_item_id_id').value = itemObj.id;
	$('pitem_item_id_autocompleter').value = itemObj.code;
	$('idProjectItemName').value = itemObj.name;
	$('pitem_quantity_unit').value = itemObj.quantity;
	$('pitem_weight_unit').value = itemObj.weight;
	$('pitem_volume_unit').value = itemObj.volume;
	$('pitem_BL_AW').focus();
}


Calendar.enabled = true;
function doCancel(){
	go('?<?=$AppUI->getPlace()?>');
}
</script>
<div id="validation_rules" style="visibility: hidden; display: none">
	var fItemName = new LiveValidation('idProjectItemName');
	fItemName.add( Validate.Presence);

	var fidDateStampD = new LiveValidation('idDateStampD');
	fidDateStampD.add( Validate.Presence);

	var fidDateCustomD = new LiveValidation('idDateCustomD');
	fidDateCustomD.add( Validate.Presence);

	var fidDateETAD = new LiveValidation('idDateETAD');
	fidDateETAD.add( Validate.Presence);

	var fidDateDeliveryD = new LiveValidation('idDateDeliveryD');
	fidDateDeliveryD.add( Validate.Presence);

	var fpitem_delivery_location = new LiveValidation('pitem_delivery_location');
	fpitem_delivery_location.add( Validate.Presence);

	new Tips($$('.text'));
</div>
<script type="text/javascript" language="javascript">
	eval($('validation_rules').innerHTML);

	function doSubmit() {
		var inNo = $('idPItemInvoiceNo');
		var poNo = $('idPItemPONo');
		var isValid = Validate.now(Validate.Presence, inNo.value)
					|| Validate.now(Validate.Presence, poNo.value);
		if (!isValid) {
			alert('Please input value for Invoice No. or PO No.');
			inNo.focus();
			return false;
		}

		var amount = $('idPItemInvoiceAmount');

		//return true if user not input invoice amount
		if (!Validate.now(Validate.Presence,  amount.value)) {
			return true;
		}

		var currency = $('idPItemCurrency');
		isValid = Validate.now(Validate.Numericality, amount.value, {minimum: 1, maximum: 9999999999});
		if (!isValid) {
			alert('Invoice amount is not valid');
			return false;
		}
		//must select currency if input invoice amount
		if(!Validate.now(Validate.Presence, currency.value)) {
			alert('Please select currency for Invoice amount');
			currency.focus();
			return false;
		}
		return isValid;
	}
</script>